package com.rc.saas.tenant.service.discount;

import com.rc.saas.tenant.common.utils.BeanUtil;
import com.rc.saas.tenant.mapper.discount.TenantDiscountPolicyMapper;
import com.rc.saas.tenant.mapper.discount.TenantDiscountStationMapper;
import com.rc.saas.tenant.mapper.discount.TenantDiscountTimeMapper;
import com.rc.saas.tenant.model.discount.*;
import com.rc.saas.tenant.vo.discount.DiscountTimeSaveVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author frog
 * @date 2019/11/25
 */
@Service
public class TenantDiscountTimeServiceImpl implements TenantDiscountTimeService {

    @Autowired
    private TenantDiscountTimeMapper tenantDiscountTimeMapper;

    @Autowired
    private TenantDiscountPolicyMapper tenantDiscountPolicyMapper;

    @Autowired
    private TenantDiscountStationMapper tenantDiscountStationMapper;

    @Override
    public TenantDiscountTimeMapper getMapper() {
        return tenantDiscountTimeMapper;
    }

    @Override
    public void insertDiscountTime(Long discountPolicyId, Long elecStationId, String discountType, String timeType, BigDecimal elecPrice, BigDecimal servicePrice, BigDecimal discount, List<DiscountTimeSaveVo> discountTimeSaveVoList, String tenantCode) throws ParseException {
        //todo 如果是更新操作删掉之前方案对应的时段
        List<TenantDiscountTime> tenantDiscountTimeList = selectTimeByPolicyId(discountPolicyId);
        List<Long> strings = new ArrayList<>();
        if (tenantDiscountTimeList != null) {
            for (TenantDiscountTime discountTime : tenantDiscountTimeList) {
                strings.add(discountTime.getDiscountTimeId());
            }
            tenantDiscountTimeMapper.deleteByBatch(strings);
        }
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        TenantDiscountTime discountTime = new TenantDiscountTime();
        discountTime.setDiscountPolicyId(discountPolicyId);
        discountTime.setElecStationId(elecStationId);
        discountTime.setTenantCode(tenantCode);

        //更改方案状态为分时段
        TenantDiscountPolicy policy = new TenantDiscountPolicy();

        //1.全天 2.分时段
        if ("1".equals(timeType)) {
            discountTime.setBeginTime(formatter.parse("00:00:00"));
            discountTime.setEndTime(formatter.parse("23:59:59"));
            //优惠类型, 0服务费打折 1 特定服务费（活动价） 2总价打折 默认0
            if ("1".equals(discountType)) {
                discountTime.setElecPrice(elecPrice);
                discountTime.setServicePrice(servicePrice);
            } else {
                discountTime.setDiscount(discount);
            }

            //默认0.整天 1.分时段
            policy.setTimeType(0);
            tenantDiscountTimeMapper.insertSelective(discountTime);
        } else {
            //默认0.整天 1.分时段
            policy.setTimeType(1);
            //前端传递的数组最后一个对象为空
            //  for (int i = 0; i < discountTimeSaveVoList.size() ; i++) {
            //      discountTime.setBeginTime(formatter.parse(discountTimeSaveVoList.get(i).getStartPeriod()));
            //      discountTime.setEndTime(formatter.parse(discountTimeSaveVoList.get(i).getEndPeriod()));
            //      if ("1".equals(discountType)) {
            //          discountTime.setElecPrice(discountTimeSaveVoList.get(i).getElecPrice());
            //          discountTime.setServicePrice(discountTimeSaveVoList.get(i).getServicePrice());
            //      } else {
            //          discountTime.setDiscount(discountTimeSaveVoList.get(i).getDiscount());
            //      }
//
            //      tenantDiscountTimeMapper.insertSelective(discountTime);
            //  }
            for (DiscountTimeSaveVo discountTimeSaveVo : discountTimeSaveVoList) {
                if (BeanUtil.isValid(discountTimeSaveVo.getStartPeriod(), discountTimeSaveVo.getEndPeriod())) {
                    discountTime.setBeginTime(formatter.parse(discountTimeSaveVo.getStartPeriod()));
                    discountTime.setEndTime(formatter.parse(discountTimeSaveVo.getEndPeriod()));
                    if ("1".equals(discountType)) {
                        discountTime.setElecPrice(discountTimeSaveVo.getElecPrice());
                        discountTime.setServicePrice(discountTimeSaveVo.getServicePrice());
                    } else {
                        discountTime.setDiscount(discountTimeSaveVo.getDiscount());
                    }

                    tenantDiscountTimeMapper.insertSelective(discountTime);
                }
            }
        }
        policy.setDiscountPolicyId(discountPolicyId);
        tenantDiscountPolicyMapper.updateByPrimaryKeySelective(policy);
    }

    @Override
    public void insertDiscountTime(Long discountPolicyId, Long elecStationId, String discountType, String timeType, BigDecimal elecPrice, BigDecimal servicePrice, BigDecimal discount, List<DiscountTimeSaveVo> discountTimeSaveVoList, String tenantCode, Long discountStationId) throws ParseException {

        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        TenantDiscountTime discountTime = new TenantDiscountTime();
        discountTime.setDiscountPolicyId(discountPolicyId);
        discountTime.setElecStationId(elecStationId);
        discountTime.setTenantCode(tenantCode);

        //更改方案状态为分时段
        TenantDiscountPolicy policy = new TenantDiscountPolicy();

        //1.全天 2.分时段
        if ("1".equals(timeType)) {
            discountTime.setBeginTime(formatter.parse("00:00:00"));
            discountTime.setEndTime(formatter.parse("23:59:59"));
            //优惠类型, 0服务费打折 1 特定服务费（活动价） 2总价打折 默认0
            if ("1".equals(discountType)) {
                discountTime.setElecPrice(elecPrice);
                discountTime.setServicePrice(servicePrice);
            } else {
                discountTime.setDiscount(discount);
            }

            //默认0.整天 1.分时段
            policy.setTimeType(0);
            tenantDiscountTimeMapper.insertSelective(discountTime);
        } else {
            //默认0.整天 1.分时段
            policy.setTimeType(1);

            for (DiscountTimeSaveVo discountTimeSaveVo : discountTimeSaveVoList) {
                if (BeanUtil.isValid(discountTimeSaveVo.getStartPeriod(), discountTimeSaveVo.getEndPeriod())) {
                    discountTime.setBeginTime(formatter.parse(discountTimeSaveVo.getStartPeriod()));
                    discountTime.setEndTime(formatter.parse(discountTimeSaveVo.getEndPeriod()));
                    if ("1".equals(discountType)) {
                        discountTime.setElecPrice(discountTimeSaveVo.getElecPrice());
                        discountTime.setServicePrice(discountTimeSaveVo.getServicePrice());
                    } else {
                        discountTime.setDiscount(discountTimeSaveVo.getDiscount());
                    }

                    tenantDiscountTimeMapper.insertSelective(discountTime);
                }
            }
        }
        policy.setDiscountPolicyId(discountPolicyId);
        tenantDiscountPolicyMapper.updateByPrimaryKeySelective(policy);

        //把之前的方案删除然后更新

        TenantDiscountStation oldTenantDiscountStation = tenantDiscountStationMapper.selectByPrimaryKey(discountStationId);

        TenantDiscountStationExample example = new TenantDiscountStationExample();
        example.createCriteria().andDiscountPolicyIdEqualTo(oldTenantDiscountStation.getDiscountPolicyId());
        List<TenantDiscountStation> tenantDiscountStationList = tenantDiscountStationMapper.selectByExample(example);

        //如果旧的方案没有关联其他桩群 将其删除 并把对应的时段也删除
        if (tenantDiscountStationList.size() <= 1) {
            tenantDiscountPolicyMapper.deleteByPrimaryKey(oldTenantDiscountStation.getDiscountPolicyId());

            TenantDiscountTimeExample tenantDiscountTimeExample = new TenantDiscountTimeExample();
            tenantDiscountTimeExample.createCriteria().andDiscountPolicyIdEqualTo(oldTenantDiscountStation.getDiscountPolicyId());
            List<TenantDiscountTime> tenantDiscountTimeList = tenantDiscountTimeMapper.selectByExample(tenantDiscountTimeExample);

            if (!CollectionUtils.isEmpty(tenantDiscountTimeList)) {
                for (TenantDiscountTime tenantDiscountTime : tenantDiscountTimeList) {
                    tenantDiscountTimeMapper.deleteByPrimaryKey(tenantDiscountTime.getDiscountTimeId());
                }
            }
        }

        tenantDiscountStationMapper.deleteByPrimaryKey(discountStationId);

        TenantDiscountStation newTenantDiscountStation = new TenantDiscountStation();
        newTenantDiscountStation.setTenantCode(tenantCode);
        newTenantDiscountStation.setElecStationId(oldTenantDiscountStation.getElecStationId());
        newTenantDiscountStation.setDiscountPolicyId(discountPolicyId);
        tenantDiscountStationMapper.insertSelective(newTenantDiscountStation);
    }


    public List<TenantDiscountTime> selectTimeByPolicyId(Long discountPolicyId) {
        TenantDiscountTimeExample example = new TenantDiscountTimeExample();
        example.createCriteria().andDiscountPolicyIdEqualTo(discountPolicyId);
        List<TenantDiscountTime> tenantDiscountTimeList = tenantDiscountTimeMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(tenantDiscountTimeList)) {
            return tenantDiscountTimeList;
        }
        return null;
    }
}
